#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

void *upload_client(void *arg)
{
	pthread_detach(pthread_self());
	char *buf;
	buf = (char *)arg;
	//printf("%s\n",buf);
	char filename[100];
	char filebuf[1024];
	int ret;
	strcpy(filename,buf+7);
	int fd = open(filename,O_RDONLY);
	if(fd == -1)
	{
		printf("not find this file : %s\n",filename);
		return NULL;
	}

	int sock = socket(AF_INET,SOCK_STREAM,0);
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(8889);
	inet_pton(AF_INET,"192.168.186.162",&addr.sin_addr.s_addr);

	if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)))
	{
		printf("connect error \n");
		close(sock);
		return 0;
	}
	//printf("client upload connect!\n");

	send(sock,buf,1024,0);

	//sleep(1);
	struct stat buf1;
	stat(filename,&buf1);
	send(sock,&buf1.st_size,4,0);

	while((ret = read(fd,filebuf,1024)) > 0)
	{
		send(sock,filebuf,ret,0);
	}
	close(fd);
	close(sock);
	printf("%s -upload success!\n",filename);

	return NULL;
}

void *download_client(void *arg)
{
	pthread_detach(pthread_self());
	char *buf;
	buf = (char *)arg;
	//printf("%s\n",buf);
	char filename[100];
	char filebuf[1024];
	int ret;
	strcpy(filename,buf+9);

	int sock = socket(AF_INET,SOCK_STREAM,0);
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(8889);
	inet_pton(AF_INET,"192.168.186.162",&addr.sin_addr.s_addr);

	if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)))
	{
		printf("connect error \n");
		close(sock);
		return 0;
	}
	//printf("client download connect!\n");

	send(sock,buf,1024,0);

	int fd = open(filename,O_WRONLY | O_CREAT,0666);
	int file_l;
	recv(sock,&file_l,4,0);
	int len,l = 0;
	while((len = recv(sock,filebuf,sizeof(filebuf),0)) > 0)
	{
		write(fd,filebuf,len);
		memset(filebuf,0,sizeof(filebuf));
		l += len;
		if(l == file_l)
			break;
	}
	close(fd);
	close(sock);
	printf("%s -download success!\n",filename);
}

int main()
{
	int sock = socket(AF_INET,SOCK_STREAM,0);
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(8888);
	inet_pton(AF_INET,"192.168.186.162",&addr.sin_addr.s_addr);

	char buf[1024];
	char order[100];
	if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)))
	{
		printf("connect error \n");
		close(sock);
		return 0;
	}
	//printf("connect success\n");
	memset(buf,0,sizeof(buf));
	recv(sock,buf,sizeof(buf),0);
	printf("%s\n",buf);

	fd_set set,oldset;
	int maxfd = sock;
	FD_ZERO(&set);
	FD_SET(0,&set);
	FD_SET(sock,&set);
	oldset = set;

	while(select(maxfd+1,&set,NULL,NULL,NULL) >= 0)
	{
		memset(buf,0,sizeof(buf));
		memset(order,0,sizeof(order));
		if(FD_ISSET(0,&set))
		{
			gets(order);
			if(!strcmp(order,"quit"))
			{	
				send(sock,order,strlen(order),0);
				break;
			}
			else if(!strncmp(order,"upload",6))
			{
				char buff[1024];
				sprintf(buff,"%s",order);
				pthread_t tid;
				pthread_create(&tid,NULL,upload_client,buff);
				set = oldset;
				continue;
			}
			else if(!strncmp(order,"download",8))
			{
				char buff[1024];
				sprintf(buff,"%s",order);
				pthread_t tid;
				pthread_create(&tid,NULL,download_client,buff);
				set = oldset;
				continue;
			}
			send(sock,order,strlen(order),0);
		}
		else if(FD_ISSET(sock,&set))
		{
			int ret = recv(sock,buf,sizeof(buf),0);
			if(ret <= 0)
			{
				printf("与服务器断开连接\n");
				break;
			}
			printf("%s\n",buf);
		}
		set = oldset;
	}
	close(sock);
}